﻿using System;
using System.Net.Sockets;
using System.Net;
using System.Windows;
using System.Threading;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
using UDPshutdown1.utils;

namespace UDPshutdown1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //MessageBox.Show(Environment.UserName);
            new Thread(() =>
            {
                // 长时间运行的操作
                Thread.Sleep(3000);

                // 使用Dispatcher更新UI
                Dispatcher.Invoke(() =>
                {
                    this.ShowInTaskbar = false;
                    this.Visibility = Visibility.Hidden;

                    AppAutoStarter.EnableAutoStart();
                });

            }).Start();

            new Thread(() =>
            {
                // 长时间运行的操作
                ReceiveData();

                // 使用Dispatcher更新UI
                Dispatcher.Invoke(() =>
                {

                });
            }).Start();


        }


        private UdpClient udpClient;
        private void ReceiveData()
        {
            udpClient = new UdpClient(8846);
            IPEndPoint? udpRemote = null;
            while (true)
            {
                try
                {
                    byte[] udpBytes = udpClient.Receive(ref udpRemote);
                    Console.Write(udpBytes.Length);
                    string mess = Encoding.Default.GetString(udpBytes);

                    switch (mess)
                    {
                        case "shutdown:r":
                            //重启
                            if (readCurrDirTxt("global.txt")!= "1")
                            {
                                break;
                            }
                            Process.Start("Shutdown.exe", " -r -t 0");
                            return;
                        case "shutdown:l":
                            //关机
                            if (readCurrDirTxt("global.txt")!= "1")
                            {
                                break;
                            }
                            Process.Start("Shutdown.exe", " -s -t 0");
                            return;
                        case "shutdown:this":
                            //关闭本程序
                            if (readCurrDirTxt("global.txt")!= "1")
                            {
                                break;
                            }
                            MessageBox.Show("已关闭UDPshutdown", "提示");
                            Environment.Exit(0);
                            return;
                        default:
                            if (mess.Contains(@"shutdown:l&user:"))
                            {
                                mess = mess.Replace(@"shutdown:l&user:", "");

                                if (readCurrDirTxt("global.txt").Equals(mess))
                                {
                                    Process.Start("Shutdown.exe", " -s -t 0");
                                    return;
                                }
                            }
                            else if (mess.Contains(@"shutdown:r&user:"))
                            {
                                mess = mess.Replace(@"shutdown:r&user:", "");

                                if (readCurrDirTxt("global.txt").Equals(mess))
                                {
                                    Process.Start("Shutdown.exe", " -r -t 0");
                                    return;
                                }
                            }
                            else if (mess.Contains(@"shutdown:this&user:"))
                            {
                                mess = mess.Replace(@"shutdown:this&user:", "");

                                if (readCurrDirTxt("global.txt").Equals(mess))
                                {
                                    MessageBox.Show($"已关闭指定用户 {Environment.UserName} UDPshutdown，附加信息 {mess}", "提示");
                                    Environment.Exit(0);
                                    return;
                                }
                            }
                            else
                            {
                                //其他信息，不回应
                                MessageBox.Show(mess, "未知命令");
                            }
                            break;
                    }


                }
                catch (Exception)
                {

                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void downapp(object sender, RoutedEventArgs e)
        {




            // MessageBox.Show(txtFileContent);

            Process.Start(new ProcessStartInfo("cmd", "/c start " + readCurrDirTxt("downlink.txt")));
        }


        private string readCurrDirTxt(string currDirFile)
        {
            FileStream fs = new FileStream(Directory.GetCurrentDirectory() + @"\" + currDirFile, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            string txtFileContent = sr.ReadToEnd();
            fs.Close();
            sr.Close();
            return txtFileContent;
        }
    }
}
